package servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import models.SubjectManagement;

/**
 * Servlet implementation class CourseRegistration
 */
public class CourseRegistration extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CourseRegistration() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		try {
			SubjectManagement sm = new SubjectManagement("");
			ArrayList<String> subjectIds = sm.getSubjectIds();
			ArrayList<String> subjectNames = sm.getSubjectNames();
			ArrayList<Integer> creditNums = sm.getCredits();
			session.setAttribute("subjectIds", subjectIds);
			session.setAttribute("subjectNames", subjectNames);
			session.setAttribute("creditNums", creditNums);

		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String url = "/coursereg.jsp";
		RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		ArrayList<String> courseIds = new ArrayList<>();
		
		String id = request.getParameter("selectedCourse");
		courseIds.add(id);

		session.setAttribute("regedCourses", courseIds);
		RequestDispatcher rd = getServletContext().getRequestDispatcher(
				"/coursereg.jsp");
		rd.forward(request, response);

	}

}
